import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import org.korifey.ffalg.NumberTheory;


public class Problem18and67 {
	
	public static void main(String[] args) throws Exception {
		System.out.println(solve("Problem18.in"));
		System.out.println(solve("Problem67.in"));
	}
	
	static final int SPEC_VAL = -1;
	static long[][] arr;
	static long[][] memoized;
	
	public static long solve(int row, int col) {
		if (memoized[row][col] == SPEC_VAL) {
			memoized[row][col] = arr[row][col] + Math.max(solve(row+1, col), solve(row+1, col+1));
		}
		return memoized[row][col];
	}
		
	public static long solve(String filename) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(Problem13.class.getClassLoader().getResourceAsStream(filename)));
		String s;
		List<long[]> lst = new ArrayList<long[]>();
		int nrows = 0;
		while ((s = reader.readLine()) != null) {		
			String[] ss = s.trim().split(" ");
			long[] row = new long[ss.length];
			for (int i=0; i<ss.length; i++) row[i] = Long.parseLong(ss[i]);
			
			lst.add(row);
			nrows++;
		}
		
		arr = lst.toArray(new long[0][]);
		memoized = new long[nrows][];
		for (int i=0; i<nrows; i++) {
			memoized[i] = new long[arr[i].length];
			Arrays.fill(memoized[i], SPEC_VAL);
		}
		System.arraycopy(arr[nrows-1], 0, memoized[nrows-1], 0, arr[nrows-1].length);
		
		return solve(0, 0);
	}
	
}
